﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Report
{
    public partial class frmkafenleixiaofeitongji : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmkafenleixiaofeitongji()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //string constr = @"Data Source=127.0.0.1;Initial Catalog=BHairDB;User ID=sa;Password=";
                SqlConnection conn = new SqlConnection(constr);
                SqlConnection conn1 = new SqlConnection(constr);
                conn.Open();
                conn1.Open();
                // 首先查询卡类型
                string sql1 = "select cid,cname from card";
                SqlCommand comm = new SqlCommand(sql1, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                string fl;
                int cs;
                string time1 = dateTimePicker1.Text;
                string time2 = dateTimePicker2.Text;
                //MessageBox.Show(time1+time2);
                var dt = new DataTable();
                dt.Columns.Add(new DataColumn("分类", typeof(string)));
                dt.Columns.Add(new DataColumn("次数", typeof(int)));
                while (sdr.Read())
                {
                    fl = sdr["cname"].ToString();
                    string sql2 = string.Format("SELECT sum(xfcs) FROM [jcxf] where riqi>='{0}' and riqi<='{1}23:59:59' and card='{2}'",time1,time2,fl);
                    // MessageBox.Show(sql2);
                    //textBox1.Text = sql2;

                    SqlCommand comm1 = new SqlCommand(sql2, conn1);
                    if (comm1.ExecuteScalar().ToString() != string.Empty)
                    {
                        cs = Int32.Parse(comm1.ExecuteScalar().ToString());
                    }
                    else
                    {
                        cs = 0;
                    }
                    var row = dt.NewRow();
                    row["分类"] = fl;
                    row["次数"] = cs;
                    dt.Rows.InsertAt(row, 0);

                }
                dataGridView1.DataSource = dt;
                conn.Close();
                conn1.Close();
            }
            catch { }
            }
    }
}
